﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WEBDT.Base;
using WEBDT.ViewModel;
namespace WEBDT.Areas.Admin.Controllers
{
    public class UserPowerController : BaseController<UserPower>
    {
        //
        // GET: /Admin/User/

        public ActionResult Index()
        {
            return View();
        }
       [HttpGet]
        public override ActionResult insert()
        {
            var model = new UserPowerModel
            {
                tontai= false,
            };
            return View(model);
        }
        [HttpPost]
       public override ActionResult insert(FormCollection fr)
       {
           var httt = new UserPowerModel
           {
               TenUserPower = fr.Get("up_name"),
               tontai = false
           };
           var ht = repository.kiemtratheoten(httt.TenUserPower);
           if (ht != null)
           {
               httt.tontai = true;
               return View(httt);
           }
           var model = new UserPower
           {
               TenUserPower = httt.TenUserPower,
           };
           repository.Insert<UserPower>(model);
           return RedirectToAction("getAll");
       }

        [HttpGet]
        public override ActionResult update(int id)
        {
            var up = repository.SelectByID<UserPower>(id);
            var model = new UserPowerModel 
            {
                MaUserPower= up.MaUserPower,
                TenUserPower= up.TenUserPower,
                tontai = false,
                kiemtra= up.TenUserPower,
            };
            return View(model);
        }

        [HttpPost]
        public override ActionResult update(FormCollection fr)
        {
            var up = new UserPowerModel
            {
                MaUserPower = Convert.ToInt32(fr.Get("up_ma")),
                TenUserPower = fr.Get("up_name"),
                kiemtra = fr.Get("kiemtra"),
                tontai = false,
            };
            var model = repository.SelectByID<UserPower>(up.MaUserPower);
            model.TenUserPower = up.TenUserPower;
            repository.commitchanges();
            return RedirectToAction("getAll");
        }
    }
}
